﻿#include "total.h"
#include "ui_total.h"
#include "QSqlDatabase"
#include "QDebug"
#include "QTime"
#include "QTimer"
#include "QMessageBox"
#include "QSqlQuery"
#include "QSqlQueryModel"
#include "QSqlTableModel"
#include "QTableView"

Total::Total(QWidget *parent,account_db *input_account_db) :
    QWidget(parent),
    account_data_db(input_account_db),
    ui(new Ui::Total)
{
    ui->setupUi(this);

    m_time = new QTime;
    //设置定时器，1S更新一次时间。
    QTimer *timer1 = new QTimer;
    timer1->start(1000);
    connect(timer1,SIGNAL(timeout()),this,SLOT(Timer_Handle()));

    QString cunrrent_index = ui->comboBox->currentText();
    Show_Data(total_db_name,cunrrent_index);
}

Total::~Total()
{
    delete ui;
}

void Total::Timer_Handle()
{

    now_time = m_time->currentTime().toString();

}
/*
*功能：初始化数据库，并建立连接。此处已经建立数据库连接。
*
*/

void Total::Show_Data(QString db_name,QString group_name)
{
    QList<float> temp_list;
    QSqlDatabase db = account_data_db->get_instance_db(db_name);
    if(group_name == QStringLiteral("统计所有组"))
        temp_list = account_data_db->count_amount_all();
    else
        temp_list = account_data_db->count_amount_group(group_name);
    float temp_list2 = account_data_db->count_table_data("person");
    //设置本年、本月的账单
    QDate now_date(QDate::currentDate());
    int now_month = now_date.month();
    int now_yesr = now_date.year();
    QDate temp_month_date(now_yesr,now_month,1);
    QDate temp_next_month_date(now_yesr,now_month+1,1);
    QString temp_month = temp_month_date.toString("yyyy-MM-dd");
    QString temp_next_month = temp_next_month_date.toString("yyyy-MM-dd");
    float temp_year_amount = account_data_db->count_amount_date(QString("%1-01-01").arg(now_yesr), QString("%1-01-01").arg(now_yesr+1));
    float temp_month_amount = account_data_db->count_amount_date(temp_month, temp_next_month);
    temp_list.append(temp_list2);
    QString OM_name,IM_name,OM_date,IM_date;
    //显示汇总数
    ui->label_3->setNum(temp_list[1]);
    ui->label_4->setNum(temp_list[0]);
    ui->label_15->setNum(temp_year_amount);  //年度账单
    ui->label_14->setNum(temp_month_amount);  //月度账单
//    //显示送出最多人
//    ui->label_9->setText(OM_name);
//    ui->label_7->setNum(-Out_Max);
//    ui->label_11->setText(OM_date);
//    //显示收入最多人
//    ui->label_10->setText(IM_name);
//    ui->label_8->setNum(In_Max);
//    ui->label_12->setText(IM_date);
    //显示总人数
    ui->label_13->setNum(temp_list[2]);
}

void Total::show_groups(QStringList str_list)
{
    combo_items = str_list;
    ui->comboBox->clear();
    ui->comboBox->addItems(str_list);
    ui->comboBox->addItem(QStringLiteral("统计所有组"));
}
/*
*功能：查看全部信息。
*
*/
void Total::on_pushButton_clicked()
{

    QSqlDatabase db = account_data_db->get_instance_db(total_db_name);
    QSqlTableModel *model = new QSqlTableModel(NULL,db);
    model->setTable("relation");
    model->select();

    QTableView * view = new QTableView();
    view->setModel(model);
    view->show();
}


/*
*功能：关闭窗口按钮。
*
*/
void Total::on_pushButton_2_clicked()
{
     emit Delate_Win("total");
}

/*
*功能：手动刷新数据按钮。
*
*/
void Total::on_pushButton_3_clicked()
{
    QString current_str = ui->comboBox->currentText();
    Show_Data(total_db_name,current_str);
    emit Text_Message(now_time+" "+QStringLiteral("已刷新！"));
}

void Total::on_comboBox_currentIndexChanged(const QString &arg1)
{
    qDebug()<<"arg1"<<arg1;
    Show_Data(total_db_name,arg1);
}
